package com.feicheng.iou.controller;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.PaymentOptions;
import com.feicheng.iou.entity.ProjectService;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.ResourcePOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.ProjectServiceService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/projectService")
@Slf4j
@Setter
@CrossOrigin
public class ProjectServiceController {

    @Autowired
    private ProjectServiceService projectServiceService;

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:30 下午 2021/9/1
     */
    @GetMapping("/list")
    public ResultInfoV110 selectProjectServiceList() {  //查询线索列表
        log.info("selectProjectServiceList方法进入");

        //调用ResourceService实现线索列表查询业务逻辑
        return projectServiceService.selectProjectServiceList();
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:30 下午 2021/9/1
     */
    @GetMapping("/listPage")
    public ResultInfoV110 selectProjectServiceListPage(Integer page, Integer size, String projectServiceStr) {  //查询线索列表
        log.info("selectProjectServiceListPage方法进入");

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(projectServiceStr);
        ProjectService projectService = jsonObject.toJavaObject(ProjectService.class);

        //调用ResourceService实现线索列表查询业务逻辑
        return projectServiceService.selectProjectServiceListPage(page, size, projectService);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:29 下午 2021/9/1
     */
    @GetMapping("/info/{id}")
    public ResultInfoV110 selectProjectServiceInfo(@PathVariable("id") Integer id) {  //查询线索列表
        log.info("selectProjectServiceInfo方法进入" + id);

        //调用ResourceService实现线索列表查询业务逻辑
        return ResultInfoV110.success(projectServiceService.selectProjectServiceByid(id));
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:29 下午 2021/9/1
     */
    @PostMapping
    public ResultInfoV110 addProjectService(String projectServiceStr) {
        log.info("addProjectService方法进入,参数为" + projectServiceStr);

        //判断请求是否重复
        if (!IdempotentUtils.judge(projectServiceStr, this.getClass())) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004, "添加服务项目添加"));
        }

        // 参数校验是否为空
        if (projectServiceStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "服务项目"));
        }

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(projectServiceStr);
        ProjectService projectService = jsonObject.toJavaObject(ProjectService.class);

        return projectServiceService.insert(projectService);
    }

    /**
     * @param paymentOptionStr
     * @return
     * @Author luoxin
     * @Description 根据Id修改分期付款服务信息
     * @Date 2:13 下午 2021/9/1
     */
    @PutMapping
    public ResultInfoV110 updateProjectService(String projectServiceStr) {
        log.info("updateProjectService方法进入,参数为" + projectServiceStr);

        // 参数校验是否为空
        if (projectServiceStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "服务项目"));
        }

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(projectServiceStr);
        ProjectService projectService = jsonObject.toJavaObject(ProjectService.class);

        return projectServiceService.update(projectService);
    }

    /**
     * @param id
     * @return
     * @Author luoxin
     * @Description 根据ID删除分期付款服务
     * @Date 2:15 下午 2021/9/1
     */
    @DeleteMapping("/delete/{id}")
    public ResultInfoV110 deleteProjectService(@PathVariable("id") Integer id) {
        log.info("deleteProjectService方法进入,参数为" + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "服务项目Id"));
        }

        return projectServiceService.delete(id);
    }


    @PutMapping("/status")
    public ResultInfoV110 updateProjectServiceStatus(Integer id, boolean status) {
        log.info("updateProjectServiceStatus方法进入,参数为" + id + "状态为" + status);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "服务项目ID"));
        }

        return projectServiceService.updateStatus(id, status);
    }
}

